package weWork;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import java.util.ArrayList;
import java.util.List;


public class ContactPage extends BasePage{

    public ContactPage(WebDriver driver) {
        super(driver);
    }
    //添加
    public ContactPage addMember(String account, String name, String phone) throws InterruptedException {

        this.find(By.linkText("添加成员")).click();
        this.find(By.name("username")).sendKeys(name);
        this.find(By.name("acctid")).sendKeys(account);
        this.find(By.name("mobile")).sendKeys(phone);
        this.find(By.linkText("保存")).click();
        Thread.sleep(1000);
        return this;
    }

    //修改
    public ContactPage modMember(String title,String phone){
        this.find(By.xpath(title)).click();
        this.find(By.linkText("编辑")).click();
        this.find(By.name("mobile")).clear();
        this.find(By.name("mobile")).sendKeys(phone);
        this.find(By.linkText("保存")).click();
        this.find(By.linkText("返回")).click();
        return this;
    }
    //删除
    public ContactPage deleMember(String title) throws InterruptedException {
        this.find(By.xpath(title)).click();
        this.find(By.linkText("删除")).click();
        this.find(By.linkText("确认")).click();
        Thread.sleep(1000);
        return  this;

    }
    //todo:待实现
    public ContactPage importContact(String path) throws InterruptedException {
        Thread.sleep(1000);
        this.find(By.xpath("//*[@class='ww_operationBar']//*[@class='ww_btn_PartDropdown_left']")).click();
        Thread.sleep(1000);

        this.find(By.linkText("文件导入")).click();
        this.find(By.linkText("填写通讯录模板后导入")).click();
        this.find(By.xpath("//*[@class='ww_fileImporter_fileContainer_uploadInputMask']")).sendKeys("D:/通讯录批量导入模板.xlsx");
        this.find(By.xpath("//*[@class='qui_btn ww_btn ww_btn_Large ww_btn_Blue ww_fileImporter_submit']")).click();
        Thread.sleep(6000);
        this.find(By.xpath("//*[@class='qui_btn ww_btn ww_btn_Big ww_btn_Blue']")).click();
        return this;
    }

//未定位到批量上传的成员元素
    public List<String> getCurrentMemberList(){
        List<String> users=new ArrayList<>();
        driver.findElements(By.cssSelector("#member_list td:nth-child(2)>span")).forEach(e->{
            users.add(e.getText());
        });
//        driver.findElements(By.cssSelector("#js_contacts12   td:nth-child(2) > span")).forEach(e->{
//            users.add(e.getText());
//        });
        return users;

    }
    public List<String> getCurrentPhoneList(){
        List<String> phoneNum=new ArrayList<>();
        driver.findElements(By.cssSelector("#member_list  td:nth-child(5) > span")).forEach(e->{
            phoneNum.add(e.getText());
        });
        return phoneNum;
    }

    public AppPage toApp(){
        this.find(By.linkText("应用管理")).click();
        return new AppPage(driver);
    }
}
